home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / cstdio.arc / SRC.ARC / PRINTF.C < prev    next >
C/C++ Source or Header  |  1984-12-10  |  3KB  |  153 lines

  1. /*    printf.c - formatted output conversion.
  2.     (C) Copyright 1984 Gregory R. Mansfield - All Rights Reserved.
  3.     G. R. Mansfield.  84/06/07.
  4.     Ver 1.2-4C10.
  5. */
  6.  
  7. #include <defstd.h>
  8. #include <stdio.h>
  9. #include <ctype.h>
  10.  
  11. printf(s, a)    /* formatted output conversion to stdout */
  12. char *s, *a;
  13. {
  14.     _printf(stdout, s, &a);
  15. }
  16.  
  17. fprintf(fp, s, a)    /* formatted output conversion to stream */
  18. FILE *fp;
  19. char *s, *a;
  20. {
  21.     _printf(fp, s, &a);
  22. }
  23.  
  24. sprintf(d, s, a)    /* formatted output conversion to string */
  25. char *s, *a;
  26. {
  27.     stdbfr._cnt = 32000;
  28.     stdbfr._ptr = d;
  29.     _printf(stdbfr, s, &a);
  30.     putc('\0', stdbfr);
  31. }
  32.  
  33. _printf(fp, s, ap)    /* output conversion formatter */
  34. FILE *fp;
  35. char *s, **ap;
  36. {
  37.     char c, pad, *sp, st[128];
  38.     int l, left, maxchr, width;
  39.     long *lp;
  40.  
  41.     while (*s) {
  42.         if (*s != '%') {    /* copy text string */
  43.             if (fp != stdbfr && *s == '\n')
  44.                 putc('\r', fp);
  45.             putc(*s++, fp);
  46.         }
  47.         else {
  48.             s++;
  49.             if (*s == '%') {
  50.                 putc(*s++, fp);
  51.                 continue;
  52.             }
  53.  
  54.             sp = st;
  55.             left = maxchr = width = 0;
  56.             pad = ' ';
  57.             if (*s == '-') {    /* left justified */
  58.                 left = 1;
  59.                 s++;
  60.             }
  61.             if (*s == '0') {    /* '0' fill */
  62.                 pad = '0';
  63.                 s++;
  64.             }
  65.             if (isdigit(*s)) {    /* width specification */
  66.                 width = atoi(s);
  67.                 while (isdigit(*s))
  68.                     s++;
  69.             }
  70.             else if (*s == '*') {    /* width from argument list */
  71.                 width = *ap++;
  72.                 s++;
  73.             }
  74.             if (*s == '.') {    /* maximum field size */
  75.                 s++;
  76.                 if (isdigit(*s)) {    /* from string */
  77.                     maxchr = atoi(s);
  78.                     while (isdigit(*s))
  79.                         s++;
  80.                 }
  81.                 else if (*s == '*') {    /* from argument list */
  82.                     width = *ap++;
  83.                     s++;
  84.                 }
  85.             }
  86.             c = *s++;
  87.             if (c == 'l') {
  88.                 c = toupper(*s);
  89.                 s++;
  90.             }
  91.     
  92.             switch (c) {    /* convert argument */
  93.             case 'D':    /* integer to decimal */
  94.                 lp = ap++;
  95.                 ltoa(*lp, sp);
  96.                 break;
  97.             case 'O':    /* integer to octal */
  98.                 lp = ap++;
  99.                 ltoab(*lp, sp, 8L);
  100.                 break;
  101.             case 'U':    /* unsigned to decimal */
  102.                 lp = ap++;
  103.                 ltoab(*lp, sp, 10L);
  104.                 break;
  105.             case 'X':    /* integer to hexadecimal */
  106.                 lp = ap++;
  107.                 ltoab(*lp, sp, 16L);
  108.                 break;
  109.             case 'c':    /* character */
  110.                 *st = *ap;
  111.                 st[1] = '\0';
  112.                 break;
  113.             case 'd':    /* integer to decimal */
  114.                 itoa(*ap, sp);
  115.                 break;
  116.             case 'o':    /* integer to octal */
  117.                 itoab(*ap, sp, 8);
  118.                 break;
  119.             case 's':    /* string */
  120.                 sp = *ap;
  121.                 break;
  122.             case 'u':    /* unsigned to decimal */
  123.                 itoab(*ap, sp, 10);
  124.                 break;
  125.             case 'x':    /* integer to hexadecimal */
  126.                 itoab(*ap, sp, 16);
  127.                 break;
  128.             default :
  129.                 *st = '\0';
  130.                 break;
  131.             }
  132.             ap++;
  133.  
  134.             if (!(l = strlen(sp)) && c == 'c')
  135.                 l++;
  136.             if (maxchr && maxchr < l)
  137.                 l = maxchr;
  138.             width = (width > l) ? width - l : 0;
  139.             if (!left)
  140.                 while (width--)
  141.                     putc(pad, fp);
  142.             while (l--) {
  143.                 if (fp != stdbfr && *sp == '\n')
  144.                     putc('\r', fp);
  145.                 putc(*sp++, fp);
  146.             }
  147.             if (left)
  148.                 while (width--)
  149.                     putc(pad, fp);
  150.         }
  151.     }
  152. }
  153.